package com.bom.boot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import com.bom.boot.core.Result;
import com.bom.boot.facade.dto.EmptyDTO;
import com.bom.boot.facade.dto.UtilDockerJavaDTO;
import com.bom.boot.facade.dto.UtilRabbitmqAddDTO;
import com.bom.boot.facade.vo.UtilDockerJavaVO;
import com.bom.boot.facade.vo.UtilRabbitmqAddVO;
import com.bom.boot.facade.vo.UtilRabbitmqMqttVO;
import com.bom.boot.service.UtilService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.time.LocalDateTime;
import java.util.Map;

@Service
@Slf4j
public class UtilServiceImpl implements UtilService {

    @Value("${util.rabbitmq.mqtt}")
    private String rabbitmqMqtt;

    @Value("${util.rabbitmq.add}")
    private String rabbitmqAdd;
    @Value("${util.java.dockerfile}")
    private String javaDockerfile;

    @Value("${util.java.compose}")
    private String javaCompose;

    @Value("${util.java.source}")
    private String javaSource;

    @Value("${util.java.version}")
    private String javaVersion;

    @Override
    public Result<UtilRabbitmqAddVO> rabbitmqAdd(UtilRabbitmqAddDTO dto) {
        UtilRabbitmqAddVO vo = new UtilRabbitmqAddVO();
        String format = StrUtil.format(rabbitmqAdd, BeanUtil.beanToMap(dto));
        vo.setCommand(format);
        return Result.<UtilRabbitmqAddVO>builder().success(vo, "成功获取添加rabbitmq").build();
    }

    @Override
    public Result<UtilRabbitmqMqttVO> rabbitmqMqtt(EmptyDTO dto) {
        UtilRabbitmqMqttVO vo = new UtilRabbitmqMqttVO();
        String format = StrUtil.format(rabbitmqMqtt, BeanUtil.beanToMap(dto));
        vo.setCommand(format);
        return Result.<UtilRabbitmqMqttVO>builder().success(vo, "启动mqtt").build();
    }

    @Override
    public UtilDockerJavaVO dockerJava(UtilDockerJavaDTO dto) {
        UtilDockerJavaVO vo = new UtilDockerJavaVO();
        String dockerfileContent = StrUtil.format(javaDockerfile, BeanUtil.beanToMap(dto));
        String composeContent = StrUtil.format(javaCompose, BeanUtil.beanToMap(dto));
        Map<String, Object> versionMap = BeanUtil.beanToMap(dto);
        versionMap.put("date", DateUtil.format(LocalDateTime.now(), DatePattern.NORM_DATETIME_PATTERN));
        String versionContent = StrUtil.format(javaVersion, versionMap);

        String serviceDir = FileUtil.getParent(javaSource, 1) + File.separator + "service";//微服务目录
        String localDir = serviceDir + File.separator + dto.getName();//当前生成服务目录
        if (FileUtil.exist(localDir)) {//删除原有服务
            FileUtil.del(localDir);
        }
        FileUtil.copy(javaSource, serviceDir, true);
        FileUtil.rename(new File(serviceDir + File.separator + "java"), dto.getName(), true);
        FileUtil.mkdir(localDir);//创建新服务目录

        //创建相关配置文件
        File dockerfile = FileUtil.touch(localDir + File.separator + "dockerfile");
        FileUtil.writeUtf8String(dockerfileContent, dockerfile);
        File compose = FileUtil.touch(localDir + File.separator + dto.getName() + ".compose");
        FileUtil.writeUtf8String(composeContent, compose);
        File version = FileUtil.touch(localDir + File.separator + dto.getName() + ".version");
        FileUtil.writeUtf8String(versionContent, version);
        File zip = ZipUtil.zip(localDir);
        vo.setZipPath(zip.getAbsolutePath());
        vo.setDirPath(localDir);
        return vo;
    }
}
